package com.nf.health.app.adapter;

import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.PictureActivity;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.models.EcgHistory;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 恩福 on 2016/9/28.
 */

public class EcgHitoryListAdapter extends RecyclerView.Adapter<EcgHitoryListAdapter.MyViewHolder>{
    private Context context;
    private OnItemDelClickListener mOnItemClickLitener;
    private boolean isOpen; // 换成Bean类中的 否则复用出错
    private List<EcgHistory> ecgHistoryList;
    private int screenWidth = 0;
    public EcgHitoryListAdapter(Context context, List<EcgHistory> ecgHistoryList) {
        this.context = context;
        this.ecgHistoryList = ecgHistoryList;
        screenWidth =  DensityUtil.getScreenIntWidth(context);
    }
    public void setEcgHitoryListAdapterDataChanged(List<EcgHistory> ecgHistoryList) {
        this.ecgHistoryList = ecgHistoryList;
        notifyDataSetChanged();
    }
    @Override
    public EcgHitoryListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.ecg_list_item,null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final EcgHistory ecgHistory = ecgHistoryList.get(position);
         if(ecgHistory.isOpen() == true || position ==0){
             holder.ll_show_ecg_content.setVisibility(View.VISIBLE);
             holder.ll_show_ecg_content.setVisibility(View.VISIBLE);
             ecgHistory.setOpen(true);
             holder.iv_ecg_triangle.setImageResource(R.drawable.iv_blue_arrowup);
        }else{
             holder.ll_show_ecg_content.setVisibility(View.GONE);
        }
        if(ecgHistory.isEditStatus() == true){//编辑（去删除）
            ValueAnimator valueAnimator = ValueAnimator.ofInt(screenWidth- holder.iv_ecg_triangle.getMeasuredWidth(),screenWidth);
            valueAnimator.setDuration(1000);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float f = animation.getAnimatedFraction();
                    int rowX = (int) animation.getAnimatedValue();
                    int rowY = (int) animation.getAnimatedValue();
                    int left = rowX - holder.iv_ecg_triangle.getWidth() / 2;
                    int top = rowY - holder.iv_ecg_triangle.getHeight() / 2;
                    int right = left + holder.iv_ecg_triangle.getWidth();
                    int bottom = top + holder.iv_ecg_triangle.getHeight();
                    holder.iv_ecg_triangle.layout(left, top, right, bottom);
                }
            });
            valueAnimator.start();
            holder.iv_ecg_triangle.setVisibility(View.GONE);
            holder.iv_ecg_del.setVisibility(View.VISIBLE);
        }else{//禁止编辑
            holder.iv_ecg_triangle.setVisibility(View.VISIBLE);
            holder.iv_ecg_del.setVisibility(View.GONE);
        }
        GlideUtil.loadNetImg2(context,holder.tv_ecg_pic,ecgHistory.getPicsurl(),R.drawable.default_square_image);
        holder.tv_ecg_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                ArrayList<String> urls = new ArrayList<String>();
                urls.add(ecgHistory.getPicsurl());
                bundle.putStringArrayList("imgUrls",urls);
                bundle.putInt("position", 0);
                bundle.putInt("type", PictureActivity.TYPE_NET_IMG);
                ActivityUtils.showActivity(context, PictureActivity.class, bundle);
                urls.clear();
            }
        });
            holder.iv_ecg_triangle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(ecgHistory.isOpen()) {//去关闭
                        holder.ll_show_ecg_content.setVisibility(View.GONE);
                        holder.iv_ecg_triangle.setImageResource(R.drawable.iv_blue_arrowdown);
                        ecgHistory.setOpen(false);
                     }else {//去打开
                        holder.ll_show_ecg_content.setVisibility(View.VISIBLE);
                        ecgHistory.setOpen(true);
                        holder.iv_ecg_triangle.setImageResource(R.drawable.iv_blue_arrowup);
                    }
                }
            });
        holder.tv_ecg_time.setText(TextUtils.isEmpty(ecgHistory.getPosttime()) ? "未知时间" : ""+ecgHistory.getPosttime() );
        holder.iv_ecg_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickLitener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        int i = ecgHistoryList == null ? 0 : ecgHistoryList.size();
        return i;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_ecg_tip;
        ImageView tv_ecg_pic;

        LinearLayout ll_show_ecg_content;
        RelativeLayout rl_content_close1;
        ImageView iv_ecg_triangle;
        ImageView iv_ecg_del;//go to delete

        TextView tv_ecg_time;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_ecg_pic =  (ImageView) itemView.findViewById(R.id.tv_ecg_pic);
            ll_show_ecg_content = (LinearLayout) itemView.findViewById(R.id.ll_show_ecg_content);
            rl_content_close1 = (RelativeLayout) itemView.findViewById(R.id.rl_content_close1);
            tv_ecg_time = (TextView) itemView.findViewById(R.id.tv_ecg_time);
            iv_ecg_triangle = (ImageView) itemView.findViewById(R.id.iv_ecg_triangle);
            tv_ecg_tip = (TextView) itemView.findViewById(R.id.tv_ecg_tip);
            iv_ecg_del = (ImageView) itemView.findViewById(R.id.iv_ecg_del);
        }
    }


    //监听事件
    public interface OnItemDelClickListener
    {
        void onItemClick(int position);
    }


    public void setOnItemClickLitener(OnItemDelClickListener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

}
